package com.zhong.gang.app.ui.zhuchang.detail;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.databinding.ActivityZhuChangBinding;
import com.zhong.gang.applib.base.act.ActBase;
import com.zhong.gang.applib.bean.MaterialBean;
import com.zhong.gang.applib.controller.ImgController;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.ui.adapter.AddImgAdapter;

import java.util.Locale;

public class ZhuChangDetailAct extends ActBase<ActivityZhuChangBinding> {
    private static final String ID = "ID";
    private ImgController imgController;
    private AddImgAdapter zjAdapter, bdAdapter;

    public static final void openAct(Context context, int id) {
        Intent intent = new Intent(context, ZhuChangDetailAct.class);
        intent.putExtra(ID, id);
        context.startActivity(intent);
    }

    @Override
    public boolean showLoading() {
        return true;
    }

    @Override
    public int LayoutRes() {
        return R.layout.activity_zhu_chang;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "驻厂接单详情";
    }

    @Override
    public void initView() {
        imgController = new ImgController();
        zjAdapter = imgController.initImgAdapter(this, bind.zjRecyclerView, 1);
        bdAdapter = imgController.initImgAdapter(this, bind.bdRecyclerView, 1);
        bind.shipperView.showEdt(false);
        bind.sure.setVisibility(View.GONE);
        bind.delete.setVisibility(View.GONE);
        bind.zhangkouLayout.setAction(false);
        bind.snLayout.setVisibility(View.VISIBLE);
        bind.snLine.setVisibility(View.VISIBLE);
        bind.moneny.setEnabled(false);
        bind.jWeightView.setEnabled(false);
        bind.mWeight.setEnabled(false);
        bind.pWeight.setEnabled(false);
        bind.kouWeight.setEnabled(false);
        bind.buttonSwitch.setEnabled(false);
        bind.zhangkouLayout.setEnabled(false);
        getData();
    }

    @Override
    public void getData() {
        super.getData();
        HttpUtils.get(Api.factoryDetail, this)
                 .addParams("orderId", getIntent().getIntExtra(ID, 0) + "")
                 .build().execute(this.new ActCallBack<BaseDataBean<ZhuChangDetialBean>>() {
            @Override
            public void onResponse(BaseDataBean<ZhuChangDetialBean> data) throws Exception {
                super.onResponse(data);
                if (data.getResult() == null)
                    return;
                bind.sn.setText(data.getResult().getSn());
                bind.shipperView.setData(data.getResult().getConsignor());
                bind.pWeight.setText(data.getResult().getTareWeight() + "");
                bind.mWeight.setText(data.getResult().getGrossWeight() + "");
                bind.jWeight.setText(String.format(Locale.CHINA, "%.2f",
                        (float) (data.getResult().getGrossWeight() -
                                data.getResult().getTareWeight())));
                bind.buttonSwitch.setChecked(
                        !TextUtils.isEmpty(data.getResult().getQualityPhoto()));
                imgController.addImgView(zjAdapter, data.getResult().getQualityPhoto());
                imgController.addImgView(bdAdapter, data.getResult().getPoundOrderPhoto());
                bind.kouWeight.setText(data.getResult().getSlagWeight() + "");
                bind.jsWeight.setText(data.getResult().getFinalWeight() + "");
                bind.moneny.setText(data.getResult().getFinalAmount() + "");
                bind.zhangkouLayout.setCheckStatus(data.getResult().getUpDownType() != 0);
                bind.zhangkouLayout.getBinding().zhangkouType.setText(
                        data.getResult().getUpDownType() == 1 ? "涨" : "扣");
                bind.zhangkouLayout.getBinding().monenyEdt.setText(
                        data.getResult().getUpDownMoney() + "");
                for (MaterialBean bean : data.getResult().getOrderMaterialTypeDetailList()) {
                    bind.shoufaLayout.addView(bean);
                }
                bind.zhangkouLayout.setEnabled(false);
                bind.snLine.setVisibility(View.VISIBLE);
                bind.snLayout.setVisibility(View.VISIBLE);
                if (data.getResult().getContract() == null) {
                    bind.contractLayout.setVisibility(View.GONE);
                    bind.contractLine.setVisibility(View.GONE);
                    return;
                } else {
                    bind.contractLayout.setVisibility(View.VISIBLE);
                    bind.contractLine.setVisibility(View.VISIBLE);
                    bind.contractTv.setText(data.getResult().getContract().getSn());
                }
            }
        });
    }
}
